package com.java9s.goldchit.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.java9s.goldchit.beans.Customer;

public class CustomerDaoImpl implements CustomerDao {

	private SessionFactory sessionFactory;
	private Session session;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public void saveCustomer(Customer customer) {
		session = getSessionFactory().openSession();
		// Transaction transaction = session.beginTransaction();
		if (customer != null) {
			session.clear();
			session.save(customer);
			session.flush();
		}
		// transaction.commit();
		session.close();
	}

	@Override
	public void updateCustomer(Customer customer) {
		session = getSessionFactory().openSession();
		// Transaction transaction = session.beginTransaction();
		if (customer != null) {
			session.clear();
			session.saveOrUpdate(customer);
			session.flush();
		}
		// transaction.commit();
		session.close();

	}

	@Override
	public Customer getCustomer(int customerId) {
		session = getSessionFactory().openSession();
		Customer customer = (Customer) session.get(Customer.class,
				Integer.valueOf(customerId));
		return customer;
	}

}
